/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package smartgrid;

import org.orm.PersistentException;
public class PriceProcessor {
	private int ID;
	
	private int StartHour;
	
	private int StartMinute;
	
	private int EndHour;
	
	private int EndMinute;
	
	private float Euros;
	
	private String action="";
	
	public void setID(int value) {
		this.ID = value;
	}
	
	public int getID() {
		return ID;
	}
	
	/**
	 * Hora a partit da qual o preço se aplica.
	 */
	public void setStartHour(int value) {
		this.StartHour = value;
	}
	
	public int getStartHour() {
		return StartHour;
	}
	
	/**
	 * Hora a partir da qual o preço deixa de ser aplicado.
	 */
	public void setStartMinute(int value) {
		this.StartMinute = value;
	}
	
	public int getStartMinute() {
		return StartMinute;
	}
	
	public void setEndHour(int value) {
		this.EndHour = value;
	}
	
	public int getEndHour() {
		return EndHour;
	}
	
	public void setEndMinute(int value) {
		this.EndMinute = value;
	}
	
	public int getEndMinute() {
		return EndMinute;
	}
	
	/**
	 * Valor do preço em euros.
	 */
	public void setEuros(float value) {
		this.Euros = value;
	}
	
	public float getEuros() {
		return Euros;
	}
	
	public String getAction() {
		return action;
	}
	
	public void setAction(String action) {
		this.action = action;
	}
	
	private int Tariff_Prices_role_tariffID;
	
	public void setTariff_Prices_role_tariffID(int value) {
		this.Tariff_Prices_role_tariffID = value;
	}
	
	public int getTariff_Prices_role_tariffID() {
		return Tariff_Prices_role_tariffID;
	}
	
	public String process() {
		String result = "Unexcepted result";
		if (action.equals("search")) {
			smartgrid.Price _price = smartgrid.Price.loadPriceByORMID(getID());
			if (_price!= null) {
				copyFromBean(_price);
				result = "Search success";
			}
			else {
				result = "Search failed";
			}
		}
		else if(action.equals("insert"))  {
			try {
				smartgrid.Price _price = smartgrid.Price.createPrice();
				copyToBean(_price);
				if (_price.save()) {
					result = "Insert success";
				}
				else {
					result = "Insert failed";
				}
			}
			catch (Exception e) {
				result = "Insert error: " + e.toString();
			}
		}
		else if (action.equals("update")) {
			smartgrid.Price _price= smartgrid.Price.loadPriceByORMID(getID());
			if (_price != null) {
				copyToBean(_price);
				if (_price.save()) {
					result = "Update success";
				}
				 else {
					result = "Update failed";
				}
			}
			 else  {
				result = "Update failed";
			}
			
		}
		else if (action.equals("delete")) {
			smartgrid.Price _price = smartgrid.Price.loadPriceByORMID(getID());
			if (_price != null && _price.deleteAndDissociate()) {
				result = "Delete success";
			}
			else {
				result = "Delete failed";
			}
		}
		else if (action.equals("")) {
			result = "";
		}
		action = "";
		return result;
	}
	
	private void copyFromBean(smartgrid.Price _price) {
		setStartHour(_price.getStartHour());
		setStartMinute(_price.getStartMinute());
		setEndHour(_price.getEndHour());
		setEndMinute(_price.getEndMinute());
		setEuros(_price.getEuros());
		setID(_price.getORMID());
		
		{
			smartgrid.Tariff _tariff = _price.getTariff_Prices_role();
			if (_tariff != null) {
				setTariff_Prices_role_tariffID(_tariff.getORMID());
			}
		}
		
	}
	
	private void copyToBean(smartgrid.Price _price) {
		_price.setStartHour(getStartHour());
		_price.setStartMinute(getStartMinute());
		_price.setEndHour(getEndHour());
		_price.setEndMinute(getEndMinute());
		_price.setEuros(getEuros());
		smartgrid.Tariff _tariff_Prices_role = smartgrid.Tariff.loadTariffByORMID(getTariff_Prices_role_tariffID());
		_price.setTariff_Prices_role(_tariff_Prices_role);
	}
	
}

